#include <SoftwareSerial.h>
//转盘的
#define IN13 4
#define IN14 5

//上升螺杆的
#define IN11 6
#define IN12 7

//夹头发的
#define IN21 2
#define IN22 3

//按摩的
#define IN23 8
#define IN24 9
#define IN31 10
#define IN32 11
#define IN33 12
#define IN34 13


void setup() {
  // put your setup code here, to run once:
  pinMode(IN11,OUTPUT);
  pinMode(IN12,OUTPUT);
  pinMode(IN13,OUTPUT);
  pinMode(IN14,OUTPUT);
  pinMode(IN21,OUTPUT);
  pinMode(IN22,OUTPUT);
  pinMode(IN23,OUTPUT);
  pinMode(IN24,OUTPUT);
  pinMode(IN31,OUTPUT);
  pinMode(IN32,OUTPUT);
  pinMode(IN33,OUTPUT);
  pinMode(IN34,OUTPUT);

  digitalWrite(IN11,LOW);
  digitalWrite(IN12,LOW);
  digitalWrite(IN13,LOW);
  digitalWrite(IN14,LOW);
  digitalWrite(IN21,LOW);
  digitalWrite(IN22,LOW);

  digitalWrite(IN23,LOW);
  digitalWrite(IN24,LOW);  
  digitalWrite(IN31,LOW);
  digitalWrite(IN32,LOW);
  digitalWrite(IN33,LOW);
  digitalWrite(IN34,LOW);

  Serial.begin(9600);
}

void Twist();
void unTwist();

void loop() {
  while(Serial.available()>0){
    String inString="";
    inString += char(Serial.read());
    delay(10);      // 延时函数用于等待字符完全进入缓冲区，可以尝试没有延时，输出结果会是什么
  
  // 检查是否接收到数据，如果接收到数据，则输出该数据
  if(inString=="1"){
    Rise();
    Serial.println("1");
    delay(1000);
  } 
  if(inString=="2"){
    Descend();
    Serial.println("2");
    delay(1000);
  } 
  if(inString=="3"){
    message();
    Serial.println("3");
    delay(1000);
  } 
  if(inString=="4"){
    Unmessage();
    delay(1000);
  }
  if(inString=="5"){
    Twist();
    delay(1000);
  }
  if(inString=="6"){
    unTwist();
    delay(1000);
  }
  if(inString=="7"){
    Maojin();
    delay(1000);
  }
  if(inString=="8"){
    Unmaojin();
    delay(1000);
  }
  if(inString=="9"){
    Rorate();
    delay(1000);
  }
  if(inString=="0"){
    Unrorate();
    delay(1000);
  }
  }
}

void message(){
  digitalWrite(IN23,HIGH);
  digitalWrite(IN31,HIGH);
  digitalWrite(IN33,HIGH);
}

void Unmessage(){
  digitalWrite(IN23,LOW);
  digitalWrite(IN31,LOW);
  digitalWrite(IN33,LOW);
}


void Rise(){
  digitalWrite(IN11,HIGH);
  delay(7000);
  digitalWrite(IN11,LOW);
  delay(1000);
  }

void Descend(){
  digitalWrite(IN12,HIGH);
  delay(7000); 
  digitalWrite(IN12,LOW);
  delay(1000);
  }

void Maojin(){
  digitalWrite(IN14,HIGH);
  delay(4000);
  digitalWrite(IN14,LOW);
  delay(2000); 
  }

void Unmaojin(){
  //毛巾夹张开
  digitalWrite(IN13,HIGH);
  delay(4000);
  digitalWrite(IN13,LOW);
  delay(2000); 
  }

void Rorate(){
  digitalWrite(IN21,HIGH);
  delay(10000);
  digitalWrite(IN21,LOW);
  delay(2000); 
  }  

void Unrorate(){
  //底盘逆向转动
  digitalWrite(IN22,HIGH);
  delay(10000);
  digitalWrite(IN22,LOW);
  delay(2000); 
  }
void Twist(){
  //上升螺杆
  Rise();
  //毛巾夹夹持
  Maojin();
  //底盘转动
  Rorate(); 
  //螺杆下降
  Descend();
  delay(3000);
  }
//返回原位置
void unTwist(){
  Rise();
  //底盘逆向转动
  Unrorate();
  //毛巾夹夹持
  Unmaojin();
  //螺杆下降
  Descend();
  delay(3000);
  } 
